//
//  ALPSortedDetailViewController.m
//  AlpFM
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 Alp. All rights reserved.
//

#import "ALPSortedDetailViewController.h"
#import "DetailCell.h"
#import "ALPAppTools.h"
#import "DetailModel.h"
#import "DetailCell1.h"
#import "BigCell.h"
@interface ALPSortedDetailViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong)UICollectionView *collection;
@property (nonatomic, strong)NSMutableArray *
titleArray;
@property (nonatomic, strong)UICollectionView *myCollection;


@end

@implementation ALPSortedDetailViewController

-(void)viewDidLoad {
    self.automaticallyAdjustsScrollViewInsets = NO;
//    self.navigationController.navigationBar.translucent = NO;
    [self loadData];
    [self creatCollection];
    [self createDetailCollection];
    self.titleArray = [NSMutableArray array];
}
//请求数据
//请求数据
- (void)loadData{
    NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=%ld&contentType=album&device=iPhone&scale=2&version=4.3.26", (long)self.myId];
    [ALPAppTools creatingDataTask:url parameters:nil successAFNBlock:^(id resuposeObject) {
        NSLog(@"%@", resuposeObject);
        NSDictionary *dictitle = resuposeObject;
        NSDictionary *dictitle1 = [dictitle objectForKey:@"tags"];
        NSArray *array = [dictitle1 objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            DetailModel *detailModel = [[DetailModel alloc]init];
            [detailModel setValuesForKeysWithDictionary:dic];
            [self.titleArray addObject:detailModel];
            NSLog(@"%lu", (unsigned long)self.titleArray.count);
        }
        [self.collection reloadData];
        [self.myCollection reloadData];
    }];
}
#pragma mark -- 创建collection标题
-(void)creatCollection {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(110, 44);
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375, 44)collectionViewLayout:layout];
    [self.collection registerClass:[DetailCell class] forCellWithReuseIdentifier:@"cell"];
    [self.collection registerClass:[DetailCell1 class] forCellWithReuseIdentifier:@"cell1"];
    self.collection.showsHorizontalScrollIndicator = NO;//隐藏滚动条

    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.bounces = YES;
    self.collection.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.collection];
    NSIndexPath *indexPath =[NSIndexPath indexPathForRow:0 inSection:0];
    [self.collection selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
}
#pragma mark -- 创建collection内容
- (void)createDetailCollection{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(375, 623);
    
    self.myCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 44, 375, 623)collectionViewLayout:layout];
    [self.myCollection registerClass:[BigCell class] forCellWithReuseIdentifier:@"cellDetail1"];
    [self.myCollection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellDetail"];

    self.myCollection.pagingEnabled = YES;
    self.myCollection.delegate = self;
    self.myCollection.dataSource = self;
    self.myCollection.bounces = YES;
    [self.view addSubview:self.myCollection];
}
//#pragma mark -- collection页面内容协议
//- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
//    return 1;
//}
//- (NSInteger)

#pragma mark -- collection标题协议
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    if (collectionView == self.collection) {
        return 1;
    }else{
        return 1;
    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView == self.collection) {
        return self.titleArray.count + 1;
    }else{
        return self.titleArray.count + 1;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.collection) {
        if (indexPath.item == 0) {
            //标题第一个cell
            DetailCell1 *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
            cell1.backgroundColor = [UIColor whiteColor];
            return cell1;
        }else{
            //标题其他cell
            DetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor whiteColor];
            cell.detailModel = self.titleArray[indexPath.item - 1];
            NSLog(@"%@", self.titleArray);
            return cell;
        }
        
    }else{
        //内容第一个cell
        if (indexPath.section == 0) {
            BigCell *cellDetail1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellDetail1" forIndexPath:indexPath];
            cellDetail1.backgroundColor = [UIColor colorWithRed:(arc4random() % 256 / 255.0) green:(arc4random() % 256 / 255.0) blue:(arc4random() % 256 / 255.0) alpha:1];
            return cellDetail1;
        }else{
            //内容其他cell
            UICollectionViewCell *cellDetail = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellDetail" forIndexPath:indexPath];
            cellDetail.backgroundColor = [UIColor colorWithRed:(arc4random() % 256 / 255.0) green:(arc4random() % 256 / 255.0) blue:(arc4random() % 256 / 255.0) alpha:1];
            return cellDetail;
        }
        
    }
}

//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    if (collectionView == self.collection) {
//        self.myCollection.contentOffset = CGPointMake(indexPath.row * 375, 0);
//    }else{
//        NSLog(@"%ld", (long)indexPath.row);
//    }
//}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    //点击标题栏上的标题 实现切换效果
    if (collectionView==self.collection) {
        
        [collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
//        NSIndexPath *viewIndexPath=[NSIndexPath indexPathForItem:0 inSection:indexPath.row];
        [self.myCollection scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if ([scrollView isEqual:self.myCollection]) {
        //计算偏移量
        CGFloat x = scrollView.contentOffset.x;
        NSInteger i = x/[UIScreen mainScreen].bounds.size.width;
        [self.collection selectItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
